/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.uct.client  
 */
package com.apache.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.vo.ResultEntity;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.Root;

/**
 * description:  兼容1.3.0uct工具类
 * @author dwusoftwu 创建时间：2015-10-28  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class UctOldCoreClient {

	/**
	 * 统一加载用户中心的统一配置
	 * @return
	 */
	public static Root getConf() {
		Map<String, String> map = UctCoreClient.getConfig();
		Root root = buildRoot(map, "获取成功");
		return root;
	}

	private static Root buildRoot(Object obj, String msg) {
		Root root = null;
		if (null != obj) {
			root = new Root();
			root.setResult("T");
			root.setEntity(obj);
			root.setInfo(msg);
		} else {
			root = Root.getRootInstance();
			root.setResult("F");
			root.setInfo(msg);
		}
		return root;
	}

	/**
	 * description:  获取用户登陆信息 
	 * @param map user.userEname,token
	 */
	public static Root getLoginUserInfoSso(Map<String, String> map) {
		LoginUser loginUser = UctCoreClient.getLoginUserFromUserCenterSso(map.get("user.userEname"), map.get("token"));
		Root root = buildRoot(loginUser, "获取成功");
		return root;
	}

	/**
	 * description:  获取用户登陆信息
	 * @param map token
	 */
	public static Root getLoginUserInfoSsoByToken(Map<String, String> map) {
		LoginUser loginUser = UctCoreClient.getLoginUserFromUserCenterSso("", map.get("token"));
		Root root = buildRoot(loginUser, "获取成功");
		return root;
	}

	/**
	 * 根据属性获取资源信息
	 * @param propertyName
	 * @param propertyName
	 * @return
	 */
	public static Root getLockByProperty(String propertyName, String propertyValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(propertyName.replaceAll("lock\\.", ""), propertyValue);
		ResultEntity entity = UctCoreClient.searchLockByReflect(map);
		Root root = null;
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			root = buildRoot(((List) entity.getEntity()).get(0), entity.getMessage());
		} else {
			root = Root.getRootInstance();
			root.setResult("F");
			root.setInfo("与用户中心通讯异常");
		}
		return root;
	}

	/**
	 * 通过反射获取资源信息
	 * @return
	 */
	public static Root searchLockByReflect(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			if ("pageNo".equals(key)) {
				maps.put("pageIndex", map.get(key));
			} else {
				maps.put(key.replaceAll("lock\\.", ""), map.get(key));
			}
		}
		ResultEntity entity = UctCoreClient.searchLockByReflect(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 根据属性查询权限信息
	 * @param propertyName
	 * @param propertyValue
	 * @return
	 */
	public static Root getActByProperty(String propertyName, String propertyValue) {
		ResultEntity entity = UctCoreClient.getActByProperty(propertyName.replaceAll("act\\.", ""), propertyValue);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 通过反射获取权限信息
	 * @param map
	 * @return
	 */
	public static Root searchActByReflect(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			if ("pageNo".equals(key)) {
				maps.put("pageIndex", map.get(key));
			} else {
				maps.put(key.replaceAll("act\\.", ""), map.get(key));
			}
		}
		ResultEntity entity = UctCoreClient.searchActByReflect(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 通过反射获取权限信息
	 * @param map
	 * @return
	 */
	public static Root searchActByRoleIdOrUserId(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		if (StrUtil.isNotNull(map.get("targetUserId"))) {
			maps.put("userId", map.get("targetUserId"));
		}
		if (StrUtil.isNotNull(map.get("roleId"))) {
			maps.put("roleId", map.get("roleId"));
		}
		ResultEntity entity = UctCoreClient.searchActByReflect(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 根据属性获取部门信息
	 * @param propertyName
	 * @param propertyValue
	 * @return
	 */
	public static Root getDeptByProperty(String propertyName, String propertyValue) {
		ResultEntity entity = UctCoreClient.getDeptByProperty(propertyName.replaceAll("dept\\.", ""), propertyValue);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 通过反射查询部门信息
	 * @param map
	 * @return
	 */
	public static Root searchDeptByReflect(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			if ("pageNo".equals(key)) {
				maps.put("pageIndex", map.get(key));
			} else {
				maps.put(key.replaceAll("dept\\.", ""), map.get(key));
			}
		}
		ResultEntity entity = UctCoreClient.searchDeptByReflect(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 根据属性查询机构信息
	 * @param propertyName
	 * @param propertyValue
	 * @return
	 */
	public static Root getOrgByProperty(String propertyName, String propertyValue) {
		ResultEntity entity = UctCoreClient.getOrgByProperty(propertyName.replaceAll("org\\.", ""), propertyValue);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 通过反射查询机构信息
	 * @param map
	 * @return
	 */
	public static Root searchOrgByReflect(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			if ("pageNo".equals(key)) {
				maps.put("pageIndex", map.get(key));
			} else {
				maps.put(key.replaceAll("org\\.", ""), map.get(key));
			}
		}
		ResultEntity entity = UctCoreClient.searchOrgByReflect(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 根据orgIds获取机构列表
	 * @param map
	 * @return
	 */
	public static Root getOrgListByOrgIds(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			if ("pageNo".equals(key)) {
				maps.put("pageIndex", map.get(key));
			} else {
				maps.put(key, map.get(key));
			}
		}
		ResultEntity entity = UctCoreClient.searchOrgByReflect(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 根据属性查询角色信息
	 * @param propertyName
	 * @param propertyValue
	 * @return
	 */
	public static Root getRoleByProperty(String propertyName, String propertyValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(propertyName.replaceAll("role\\.", ""), propertyValue);
		ResultEntity entity = UctCoreClient.searchRoleByReflect(map);
		Root root = null;
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			root = buildRoot(((List) entity.getEntity()).get(0), entity.getMessage());
		} else {
			root = Root.getRootInstance();
			root.setResult("F");
			root.setInfo("与用户中心通讯异常");
		}
		return root;
	}

	/**
	 * 通过反射查询角色信息
	 * @param map
	 * @return
	 */
	public static Root searchRoleByReflect(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			if ("pageNo".equals(key)) {
				maps.put("pageIndex", map.get(key));
			} else {
				maps.put(key.replaceAll("role\\.", ""), map.get(key));
			}
		}
		ResultEntity entity = UctCoreClient.searchRoleByReflect(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 通过属性查询用户信息
	 * @param propertyName
	 * @param propertyValue
	 * @return
	 */
	public static Root getUserByProperty(String propertyName, String propertyValue) {
		ResultEntity entity = UctCoreClient.getUserByProperty(propertyName.replaceAll("user\\.", ""), propertyValue);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 通过反射查询用户信息
	 * @param map
	 * @return
	 */
	public static Root searchUserByReflect(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			if ("pageNo".equals(key)) {
				maps.put("pageIndex", map.get(key));
			} else {
				maps.put(key.replaceAll("use\\.", ""), map.get(key));
			}
		}
		ResultEntity entity = UctCoreClient.searchUserByReflect(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	public static Root searchUserByReflect2Sso(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			if ("pageNo".equals(key)) {
				maps.put("pageIndex", map.get(key));
			} else {
				maps.put(key.replaceAll("user\\.", ""), map.get(key));
			}
		}
		ResultEntity entity = UctCoreClient.searchUserByReflect(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 通过userVo查询用户信息
	 * @param map
	 * @return
	 */
	public static Root searchUserByUserVo(Map<String, String> map) {
		Root root = null;
		if (StrUtil.isNull(map.get("userVo.delStatus"))) {
			root = Root.getRootInstance();
			root.setResult("F");
			root.setInfo("参数userVo.delStatus未传递");
		} else {
			Map<String, Object> maps = new HashMap<String, Object>();
			for (String key : map.keySet()) {
				if ("pageNo".equals(key)) {
					maps.put("pageIndex", map.get(key));
				} else if ("userVo.delStatus".equals(key)) {
					String status = map.get(key).equals("F") ? "1" : "-1";
					maps.put("userStatus", status);
				} else {
					maps.put(key.replaceAll("userVo\\.", ""), map.get(key));
				}
			}
			ResultEntity entity = UctCoreClient.searchUserByReflect(maps);
			root = buildRoot(entity.getEntity(), entity.getMessage());
		}
		return root;
	}

	/**
	 * 删除用户
	 * @param map
	 * @return
	 */
	public static Root deleteUserByReflect(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			if (!"user.delStatus".equals(key)) {
				maps.put(key.replaceAll("user\\.", ""), map.get(key));
			}
		}
		maps.put("userStatus", "-1");
		ResultEntity entity = UctCoreClient.editUserInfo(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 用户修改
	 * @param map
	 * @return
	 */
	public static Root updateUserByReflect(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			maps.put(key.replaceAll("user\\.", ""), map.get(key));
		}
		ResultEntity entity = UctCoreClient.editUserInfo(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 添加用户
	 * @param map
	 * @return
	 */
	public static Root saveUser(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			maps.put(key.replaceAll("user\\.", ""), map.get(key));
		}
		ResultEntity entity = UctCoreClient.saveUserInfo(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 用户密码校验及修改
	 * @param map
	 * @return
	 */
	public static Root updateUserPassword(Map<String, String> map) {
		Root root = null;
		if (StrUtil.isNull(map.get("oldPassword"))) {
			root = Root.getRootInstance();
			root.setResult("F");
			root.setInfo("参数oldPassword未传递");
		} else {
			Map<String, Object> maps = new HashMap<String, Object>();
			for (String key : map.keySet()) {
				maps.put(key.replaceAll("user\\.", ""), map.get(key));
			}
			ResultEntity entity = UctCoreClient.editUserInfo(maps);
			root = buildRoot(entity.getEntity(), entity.getMessage());
			return root;
		}
		return root;
	}

	/**
	 * 授予用户权限
	 * @return
	 */
	public static Root grantActToUser(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			maps.put(key.replaceAll("user\\.", ""), map.get(key));
		}
		ResultEntity entity = UctCoreClient.grantActToUser(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}

	/**
	 * 剥夺用户权限
	 * @param map
	 * @return
	 */
	public static Root revokeActFromUser(Map<String, String> map) {
		Map<String, Object> maps = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			maps.put(key.replaceAll("user\\.", ""), map.get(key));
		}
		ResultEntity entity = UctCoreClient.revokeActFromUser(maps);
		Root root = buildRoot(entity.getEntity(), entity.getMessage());
		return root;
	}
}
